package org.supda.system.controller.tag;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.shiro.SecurityUtils;
import org.supda.system.entity.TUser;
import org.supda.system.service.ResourceService;
import org.supda.system.util.SpringUtils;

@SuppressWarnings("serial")
public class PermissionViewTag  extends TagSupport {

	private String buttonGroup;
	private String menuId;

	public String getButtonGroup() {
		return buttonGroup;
	}

	public void setButtonGroup(String buttonGroup) {
		this.buttonGroup = buttonGroup;
	}
	
	public String getMenuId() {
		return menuId;
	}

	public void setMenuId(String menuId) {
		this.menuId = menuId;
	}

	@Override
	public int doEndTag() throws JspException {
		
		resourceService = getResourceService();
		
		TUser user =  (TUser) SecurityUtils.getSubject().getSession().getAttribute("userSession");
		List<String> permissionsTags = resourceService.findButtonsByUserName(user.getAccountName(), buttonGroup , menuId);
		
        JspWriter out = pageContext.getOut();
        try {
        	String result = "";
        	for(String permissionTag : permissionsTags){
        		result += permissionTag;
        	}
        	
            out.write(result);
            
        } catch (IOException e) {
            throw new JspException(e);
        }
        return TagSupport.EVAL_PAGE;
	}
	

    private static ResourceService resourceService;

    public static ResourceService getResourceService() {
        if(resourceService == null) {
            resourceService = SpringUtils.getBean(ResourceService.class);
        }
        return resourceService;
    }

	
}
